package prod_line

func (s *sdk) testMeResponse() {
	packet := BuildPacket(CmdCommunicateResp, []byte{0x00, 0x00})
	err := s.sendPacket(packet)
	if err != nil {
		panic(err)
	}
}

// Response 通用返回报文, biz: 业务代码(返回给哪个业务)
func (s *sdk) Response(biz byte) {
	var payload = BuildPacket(CmdResp, []byte{0x02, biz})
	err := s.sendPacket(payload)
	if err != nil {
		panic(err)
	}
}

func (s *sdk) sendCheckResult(cr CheckResult) {

	channelSize := len(cr.Items)
	var body = []byte{0x00, byte(channelSize)}

	for _, item := range cr.Items {
		var p = make([]byte, 8)
		p[0] = item.ChannelId
		copy(p[1:7], item.Uid[:])
		p[7] = item.Result
		body = append(body, p...)
	}

	var packet = BuildPacket(CmdCheckResp, body)
	err := s.sendPacket(packet)
	if err != nil {
		panic(err)
	}
}

func (s *sdk) sendCodingResult(cr CodingResult) {
	// 脚线长度
	channelSize := len(cr.Items) // 通道数量
	var body = []byte{0x00, cr.LineLength[0], cr.LineLength[1], byte(channelSize)}

	for _, item := range cr.Items {
		var p = make([]byte, 21)
		p[0] = item.ChannelId
		copy(p[1:14], item.DetonatorCode[:])
		p[14] = item.Platform
		p[15] = item.Permission
		p[16] = item.Segment[0]
		p[17] = item.Segment[1]
		p[18] = item.Delay[0]
		p[19] = item.Delay[1]
		p[20] = item.Result
		body = append(body, p...)
	}

	var packet = BuildPacket(CmdCodingResult, body)
	err := s.sendPacket(packet)
	if err != nil {
		panic(err)
	}
}

// sendPacket 发送报文
func (s *sdk) sendPacket(packet []byte) (err error) {
	n, err := session.Write(packet)
	if err != nil {
		panic(err)
	}
	if n < len(packet) {
		panic(CommunicateErr)
	}
	return
}
